package org.jeecgframework.web.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.commons.httpclient.HttpRecoverableException;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.util.ListUtils;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.web.service.CbsPartInfoServiceI;
import org.jeecgframework.web.service.CbsProcurementNoteServiceI;
import org.jeecgframework.web.system.pojo.base.*;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.util.StaticValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

/**
 * 采购订单跳转
 * 
 * @author asdc
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/purchaseorderController")
public class PurchaseorderController extends BaseController {
	/**
	 * @author lishuai 2016年8月29日 下午5:17:55 Description 跳转到采购信息表
	 * @param request
	 * @return
	 */
	@Autowired
	private CbsPartInfoServiceI cbsPartInfoService;
	@Autowired
	private SystemService systemService;

    @Autowired
    private CbsProcurementNoteServiceI cbsProcurementNoteService;

	public List<CbsPartInfoEntity> getPartInfoList(String sort, String name) {
		StringBuffer sb = new StringBuffer("from CbsPartInfoEntity where canSale=1 and canWxSale=1");
		if (StringUtil.isNotEmpty(sort)) {
			sb.append(" and commodity = '" + sort + "'");
		}
		if (StringUtil.isNotEmpty(name)) {
			sb.append(" and wechatName like '%" + name + "%'");
		}
		List<CbsPartInfoEntity> partList = systemService.findHql(sb.toString());
		for (CbsPartInfoEntity part : partList) {
			if (part.getPartImgList().size() > 0) {
				part.setFilePath(part.getPartImgList().get(0).getAllPath());
			}

		}
		return partList;
	}

	public CbsPartInfoEntity getCbsPartInfoEntity(String id) {
		CbsPartInfoEntity partinfo = systemService.get(CbsPartInfoEntity.class, id);
		return partinfo;
	}

	public void saveCbsShoppingCartEntity() {

	}

	/**
	 * 跳转到零件列表
	 * 
	 * @author lishuai 2016年8月30日 下午4:16:34 Description
	 * @param request
	 * @param sort
	 *            类型
	 * @param name
	 *            微信名
	 * @return
	 */
	@RequestMapping(params = "purchaseorder")
	public ModelAndView purchaseorder(HttpServletRequest request, String sort, String name) {
		request.setAttribute("partList", getPartInfoList(sort, name));
        TSUser user = ResourceUtil.getSessionUserName();
        int permission = 0;
        if(user != null){
            permission = oConvertUtils.getInt(user.getAuthenticationLevel());
        }
        request.setAttribute("permission",permission);
		return new ModelAndView("com/wx/added_product/procurement/partDetails");
	}

    @RequestMapping(params = "afterSubmit")
    public ModelAndView afterSubmit(String msg){
        ModelAndView mv = new ModelAndView("com/wx/added_product/procurement/after-submit");
        mv.addObject("msg",msg);
        return mv;
    }

	/**
	 * 产品详情页
	 * 
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(params = "purchaseorder_con")
	public ModelAndView goshoppart(HttpServletRequest request, String id) {
		CbsPartInfoEntity cbsPartInfoEntity = systemService.getEntity(CbsPartInfoEntity.class, id);
		request.setAttribute("partInfo", cbsPartInfoEntity);
        request.setAttribute("from",oConvertUtils.getString(request.getParameter("from")));
		return new ModelAndView("com/wx/added_product/procurement/pro_con");
	}

    /**
     * 微信订单详情页面
     * @param id
     * @param map
     * @return
     */
    @RequestMapping(params = "wxOrderDetail")
    public ModelAndView wxOrderDetail(String id, ModelMap map) {
    	String toUrl = "com/wx/personal_center/order/order_detail";
    	TSUser user = ResourceUtil.getSessionUserName();
    	if(user == null || !user.ifGoldMedal()){
    		toUrl = "com/wx/personal_center/order/order_detail_open";
		}
        CbsProcurementNoteEntity note = systemService.getEntity(CbsProcurementNoteEntity.class, id);
        map.put("note",note);
        map.put("ord", "ordinary");
        double price = 0.0;
        if(note != null &&!ListUtils.isNullOrEmpty(note.getPartList())){
            for(CbsProcurementPartEntity part :note.getPartList()){
                price += (oConvertUtils.getInt(part.getNum()) * part.getPrice());
            }
        }
        map.put("price",price);
        return new ModelAndView(toUrl);
    }

	/**
	 * @author lishuai 2016年8月30日 下午4:20:08 Description
	 * @param sort
	 * @param name
	 * @return
	 */
	@RequestMapping(params = "getPartInfoList")
	@ResponseBody
	public List<CbsPartInfoEntity> getPartInfoList1(String sort, String name) {
		return getPartInfoList(sort, name);
	}

	/**
	 * 立即购买
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(params = "buy")
	public ModelAndView buy(String id) {
		return new ModelAndView("com/wx/added_product/procurement/buy");
	}

	/**
	 * 添加到购物车
	 * 
	 * @return
	 */
	@RequestMapping(params = "addShop", produces = "application/json;charset=utf-8")
	@ResponseBody
	public JSONObject addShop(HttpServletRequest request, HttpServletResponse response) {

		return new JSONObject();
	}

	@RequestMapping(params = "shop")
	public ModelAndView shop(String userId) {
		return new ModelAndView("com/wx/added_product/procurement/shop");
	}
}
